<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<addForm v-show="addStatus" ref="addFormRef" />
		<view v-show="!addStatus">
			<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="150"
				label-align="left">
				<u-form-item label="预算编号" v-if="judgeShow('te002')">
					<u-input v-model="dataForm.te002" disabled></u-input>
				</u-form-item>
				<u-form-item label="人员" v-if="judgeShow('te004')">
					<jnpf-user-select v-model="dataForm.te004" @input="changeName" :disabled="
                      judgeWrite('te004') || setting.readonly" />
				</u-form-item>
				<u-form-item label="部门" v-if="judgeShow('te003')">
					{{dataForm.te013 }}
				</u-form-item>
				<u-form-item label="日期" v-if="judgeShow('te005')">
					<u-input v-model="dataForm.te005" disabled></u-input>
					<lys-date v-if="showte005" type="1" @cancleTime="showte005 = false"
						@submitTime="submitte005"></lys-date>
					<view style="display: flex;justify-content: space-between;width: 100%;">
						<view>
							{{dataForm.te005}}
						</view>
						<view>
							<u-icon name="arrow-down-fill" color='#ccc' @click="showte005=true" :disabled="
                      judgeWrite('te005') || setting.readonly"></u-icon>
						</view>
					</view>
				</u-form-item>
				<u-form-item label="年度" v-if="judgeShow('te006')">
					<lys-date v-if="showte006" type="4" @cancleTime="showte006 = false"
						@submitTime="submitte006"></lys-date>
					<view style="display: flex;justify-content: space-between;width: 100%;">
						<view>
							{{dataForm.te006}}
						</view>
						<view>
							<u-icon name="arrow-down-fill" color='#ccc' @click="showte006=true" :disabled="
                      judgeWrite('te006') || setting.readonly"></u-icon>
						</view>
					</view>
				</u-form-item>
				<u-form-item label="预算类型" v-if="judgeShow('te007')">
					<u-radio-group v-model="dataForm.te007" @change="closingDateChange" :disabled="
                      judgeWrite('te007') || setting.readonly">
						<u-radio v-for="item in te007Options" :key="item.id" :name="item.id">
							{{item.fullName}}
						</u-radio>
					</u-radio-group>

				</u-form-item>
				<u-form-item label="预算起始月" v-if="judgeShow('te008')">
					<lys-date v-if="showte008" type="3" @cancleTime="showte008 = false"
						@submitTime="submitte008"></lys-date>
					<view style="display: flex;justify-content: space-between;width: 100%;">
						<view>
							{{dataForm.te008}}
						</view>
						<view>
							<u-icon name="arrow-down-fill" color='#ccc' @click="showte008=true" :disabled="
                      judgeWrite('te008') || setting.readonly"></u-icon>
						</view>
					</view>
				</u-form-item>
				<u-form-item label="预算截至月" v-if="judgeShow('te009')">
					<lys-date v-if="showte009" type="3" @cancleTime="showte009 = false"
						@submitTime="submitte009"></lys-date>
					<view style="display: flex;justify-content: space-between;width: 100%;">
						<view>
							{{dataForm.te009}}
						</view>
						<view>
							<u-icon name="arrow-down-fill" color='#ccc' @click="showte009=true" :disabled="
                      judgeWrite('te009') || setting.readonly"></u-icon>
						</view>
					</view>
				</u-form-item>
				<u-form-item label="预算总金额" v-if="judgeShow('te012')">
					<u-input v-model="dataForm.te012" disabled></u-input>
				</u-form-item>
				<u-form-item label="备注" v-if="judgeShow('te011')">
					<u-input v-model="dataForm.te011" :disabled="
                      judgeWrite('te011') || setting.readonly"></u-input>
				</u-form-item>
				<view class="jnpf-table" style="margin-top: 24rpx">
					<view class="jnpf-table-item" style="margin-bottom: 24rpx"
						v-for="(item, i) in dataForm.tableField112" :key="i">
						<u-form-item label="序号" style="background-color: aqua">
							{{ i + 1 }}
						</u-form-item>
						<u-form-item label="年月" v-if="judgeShow('tableField112-tf003')">
							<lys-date v-if="showtf003" type="3" @cancleTime="showtf003 = false"
								@submitTime="submittf003"></lys-date>
							<view style="display: flex;justify-content: space-between;width: 100%;">
								<view>
									{{item.tf003}}
								</view>
								<view>
									<u-icon name="arrow-down-fill" color='#ccc' @click="Showtf003(i)" :disabled="
                      judgeWrite('tableField112') ||
                      judgeWrite('tableField112-tf003')||setting.readonly"></u-icon>
								</view>
							</view>
						</u-form-item>
						<u-form-item label=" 费用类型" v-if="judgeShow('tableField112-tf004')">
							<u-input v-model="item.tf004Name" :disabled="
                      judgeWrite('tableField112') ||
                      judgeWrite('tableField112-tf004')||setting.readonly"></u-input>
						</u-form-item>
						<u-form-item label="预算" v-if="judgeShow('tableField112-tf005')">
							<u-input v-model="item.tf005" :disabled="
                      judgeWrite('tableField112') ||
                      judgeWrite('tableField112-tf005')||setting.readonly"></u-input>
						</u-form-item>
						<u-form-item label="已发生金额" v-if="judgeShow('tableField112-tf006')">
							<u-input v-model="item.tf006" disabled></u-input>
						</u-form-item>
						<u-form-item label="备注" v-if="judgeShow('tableField112-tf007')">
							<u-input v-model="item.tf007" :disabled="
                      judgeWrite('tableField112') ||
                      judgeWrite('tableField112-tf007')||setting.readonly"></u-input>
						</u-form-item>
					</view>
				</view>
			</u-form>
		</view>

	</view>
</template>

<script>
	import addForm from "./addForm.vue";
	import comMixin from "../mixin";
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update,
	} from "@/api/apply/order";
	import {
		getOaste,
		BomcbListInit,
		putOaste,
	} from "@/api/workFlow/flowBefore";
	import {
		getDictionaryDataSelector,
		getDataInterfaceDataInfo,
		getUserAll
	} from "@/api/common";

	export default {
		mixins: [comMixin],
		components: {
			addForm
		},
		computed: {
			getTb019Computed() {
				return (id) => {
					const options = this.options.filter((item) => id.includes(item.id));
					return options.length > 0 ? options[0].fullName : "";
				};
			},
		},
		watch: {
			'dataForm.tableField112': {
				handler(newVal) {
					let total = 0;
					newVal.forEach(item => {
						if (item.tf005) {
							total += parseFloat(item.tf005);
						}
					});
					this.dataForm.te012 = total;
				},
				deep: true // 深度监听数组内部的变化
			}
		},
		data() {
			return {
				te007Options: [{
						fullName: "月度",
						id: "1"
					},
					{
						fullName: "季度",
						id: "2"
					},
					{
						fullName: "年度",
						id: "3"
					},
				],
				addStatus: '',
				department: '',
				show: false,
				opType: 0,
				dataForm: {
					te003: '',
					te004: '',
				},
				id: "",
				showForm: [],
				formOperatesList: [],
				tb019Options: [],
				options: [{
						fullName: "必选，唯一",
						id: "1",
					},
					{
						fullName: "必选，至少一个",
						id: "2",
					},
					{
						fullName: "可选，唯一",
						id: "3",
					},
					{
						fullName: "任意选",
						id: "4",
					},
				],
				showte005: false,
				showte006: false,
				showte008: false,
				showte009: false,
				showtf003: false,
				index: null,
			};
		},
		methods: {
			submitte005(time, type, timeInt) {
				let te005 = time.split("-");
				this.dataForm.te005 = te005[0] + te005[1] + te005[2];
				this.showte005 = false;
			},
			submitte006(time, type, timeInt) {
				this.dataForm.te006 = time
				this.showte006 = false;
			},
			submitte008(time, type, timeInt) {
				let te008 = time.split("-");
				this.dataForm.te008 = te008[0] + te008[1];
				this.showte008 = false;
			},
			submitte009(time, type, timeInt) {
				let te009 = time.split("-");
				this.dataForm.te009 = te009[0] + te009[1];
				this.showte009 = false;
			},
			submittf003(time, type, timeInt) {
				let tf003 = time.split("-");
				this.dataForm.tableField112[this.index].tf003 = tf003[0] + tf003[1];
				this.showtf003 = false;
			},
			Showtf003(index) {
				this.index = index;
				console.log('this.index ', this.index);
				this.showtf003 = true;
			},
			init(data, id, flowFormContentJson, addStatus) {
				this.addStatus = addStatus
				this.opType = data.opType;
				this.setting = data;
				this.formOperatesList = data.formOperates;
				this.id = id;
				if (!this.addStatus) {
					this.getInfoFn(id);
				} else {
					console.log('新增');
					this.$refs.addFormRef.getInfoFn(data)

				}
			},
			getInfoFn(id) {
				getOaste(id).then((res) => {
					if (res.code == 200) {
						this.dataForm = res.data;
						this.changeName(this.dataForm.te004);
						this.getDataInterfaceDataInfoME001(this.dataForm.te003);
						this.getDataInterfaceDataInfoMV001(this.dataForm.te004);
					}
				});
			},
			judgeShow(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return true;
				let arr = this.setting.formOperates.filter(o => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return item.read;
			},
			judgeWrite(id) {
				if (!this.setting.formOperates || !this.setting.formOperates.length)
					return false;
				let arr = this.setting.formOperates.filter(o => o.id === id) || [];
				if (!arr.length) return true;
				let item = arr[0];
				return !item.write;
			},
			closingDateChange() {
				var year = this.dataForm.te008.substring(0, 4); // 获取预算起始月的年份
				var month = this.dataForm.te008.substring(5, 7); // 获取预算起始月的月份
				const nowDate = this.dataForm.te008; //将预算起始月赋值给变量nowDate
				//预算类型为年度
				if (this.dataForm.te007 == 3) {
					//初始化预算截至月
					var m = Number(month) - 1;
					if (m == 0) {
						m = 12;
					} else {
						var y = Number(year) + 1;
						year = y.toString();
					}
					if (m >= 1 && m <= 9) {
						m = "0" + m;
					}
					month = m.toString();
					var endDate = year + month;
					this.dataForm.te009 = endDate;
				}
				//预算类型为季度
				else if (this.dataForm.te007 == 2) {
					//初始化预算截至月
					var m = Number(month) + 2;
					if (m > 12) {
						m = m - 12;
						var y = Number(year) + 1;
						year = y.toString();
					}
					if (m >= 1 && m <= 9) {
						m = "0" + m;
					}
					month = m.toString();
					var endDate = year + month;
					this.dataForm.te009 = endDate;
				}
				//预算类型为月度
				else if (this.dataForm.te007 == 1) {
					this.dataForm.te009 = this.dataForm.te008;
				}
			},
			// },
			changeName(id) {
				getUserAll().then(res => {
					let item = res.data.filter(o => o.id === id)[0]
					this.dataForm.te003 = item.departmentId; //部门编号
					this.dataForm.te013 = item.department; //部门名称
				})
			},
			queryChildBachelor(tb004) {
				let query = {
					tb004,
				};
				BomcbListInit(query).then((res) => {
					this.showForm = res.data;
					this.show = true;
				});
			},
			// 部门
			getDataInterfaceDataInfoME001(id) {
				let data = {
					id,
					interfaceId: "362111557984852549",
					propsValue: "ME001",
					relationField: "JoinStr",
				};
				getDataInterfaceDataInfo("362111557984852549", data).then((res) => {
					this.dataForm.te003 = res.data.JoinStr;
				});
			},
			// 人员
			getDataInterfaceDataInfoMV001(id) {
				let data = {
					id,
					interfaceId: "445425382452126213",
					propsValue: "MV001",
					relationField: "JoinStr",
				};
				getDataInterfaceDataInfo("445425382452126213", data).then((res) => {
					this.dataForm.te003 = res.data.JoinStr;
				});
			},
			selfGetInfo() {
				Info(this.setting.id).then((res) => {
					this.dataForm = res.data;
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson);
					}
				});
			},
			submit(eventType, config) {
				console.log('config===>', config);
				this.dataForm.flowId = config.flowId
				this.dataForm.flowState = eventType === "submit" ? 0 : 1;
				if (eventType === "audit" || eventType === "reject") {
					this.$emit("eventReceiver", this.dataForm, eventType);
					return;
				}
				let name = eventType === "submit" ? '提交' : '保存';
				uni.showModal({
					title: "提示",
					content: `您确定要${name}当前流程吗？`,
					success: (res) => {
						if (res.confirm) {
							this.selfHandleRequest();
						}
					},
				});
			},
			selfHandleRequest(flowId) {
				console.log('this.dataForm', this.dataForm);
				putOaste(this.dataForm.te001, this.dataForm).then((res) => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit("refresh");
								uni.navigateBack();
							}, 1500);
						},
					});
				});
			},
			initData() {
				getGoodsList().then((res) => {
					this.goodsOptions = res.data.list;
				});
				getCustomerList().then((res) => {
					this.customerOptions = res.data.list;
				});
			},
			selfInit() {
				this.addGoods();
				this.addPlan();
			},
			onChange(e) {
				const user = e[0];
				this.dataForm.salesmanName = user.fullName;
			},
			addGoods() {
				const item = {
					goodsId: "",
					goodsCode: "",
					goodsName: "",
					specifications: "",
					unit: "",
					qty: "",
					price: "",
					amount: "",
					discount: null,
					cess: null,
					actualPrice: "",
					actualAmount: "",
					description: "",
				};
				this.dataForm.goodsList.push(item);
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1);
			},
			addPlan() {
				const item = {
					receivableDate: "",
					receivableRate: "",
					receivableMoney: "",
					receivableMode: "",
					abstract: "",
				};
				this.dataForm.collectionPlanList.push(item);
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1);
			},
			onGoodsChange(e, i) {
				const goods = e[0];
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: "",
				};
				this.$forceUpdate();
			},
			onCustomerChange(e) {
				const customer = e[0];
				this.dataForm.customerName = customer.text;
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(
					parseFloat(row.price) * parseFloat(row.qty)
				);
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(
					discountPrice * (1 + row.cess / 100)
				);
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(
					parseFloat(row.actualPrice) * parseFloat(row.qty)
				);
				this.$forceUpdate();
			},
		},
	};
</script>